# running time in minutes
run_minutes <- 26.34
# running time in seconds
run_seconds <- run_minutes*60
# print result
run_seconds1 Introduction to the pre-sessional for R
This webpage contains instructions on how to install R on your laptop and some exercises to get you started with R.
This tutorial must be completed before you join the in-person R pre-sessional workshops run by the Digital Skills Lab (DSL).
This tutorial and follow up in-person R pre-sessional workshops are part of your pre-sessional programme. They are designed to help you get up to speed with R which you will be using in your modules. Once you have completed this tutorial, see Section 6 for information on what happens next.
1.1 Who is this pre-sessional for?
This tutorial is for all students who will be taking the following modules:
- MY360/361
- MY451A
- MY452A
- MY464
- MY470
- MY472
- MY452/552
- MY455/55/MY472
- MY457/557
- MY474/574
- MY461/561
- MY459/559
- DS105
- DS202
1.2 Tutorial Learning Outcomes
After having completed this tutorial, you will be able to:
- Install R and Rstudio
- Run R code in the console
- Open and run R scripts
- Write code in R scripts
This tutorial is a step-by-step guide to get you started. Try not to skip any of the sections. You can use the table of contents on the right hand side to help you navigate sections.
If you struggle with any of the steps from this tutorial, we recommend the following:
- Come to the in-person R pre-sessional, as detailed in Section 6.1, to get help and learn more about R
- Seek support from our online 1-2-1 or drop-in advice, as detailed in the Section 6.2 section
2 Installing R and RStudio
The first stage of this tutorial is getting R and RStudio installed on your laptop so you can start using R! Below are step by step instructions for getting the software needed installed.
2.1 Windows install
2.1.1 Install R
To install R, you need to download the installer from the R website
Click on either base or install R for the first time
Click on the download R for Windows link
Once downloaded, open the
.exefile and follow the installation instructions on your computer
2.1.2 Install RStudio
To install RStudio we download it from the Posit website
Click on the Download RStudio Desktop link
Once downloaded, open the
.exefile and follow the installation instructions on your computer
2.2 Mac install
2.2.1 Install R
To install R on your Mac you need to know the type of processor your Mac uses. This is straightforward to find out:
- On the top navigation bar on your Mac, click on the apple icon
- From the drop down menu, select About This Mac
- In Overview you will find the information about your Mac. If you have an Intel Mac, you will see the processor row, which has information that includes Intel. If you have an M1 or M2 Mac, you will see chip and M1/M2 in the Overview with something like Chip Apple M1
2.2.1.1 M1 or M2 Mac
To install R, you need to download the installer from the R website
If you have a M1 Mac you will need click on the link the contains arm64 to download R. It will look something like
R-4.3.1-arm64.pkgOnce downloaded, open the
.pkgfile and follow the installation instructions
2.2.1.2 Intel Mac
To install R, you need to download the installer from the R website
If you have a Intel Mac you will need to click on the link that just contains the version of R. It will look something like
R-4.3.1.pkg, and can be located a touch further down the page under the header Binaries for legacy macOS/OS X systems:Once downloaded, open the
.pkgfile and follow the installation instructions
2.2.2 Install XQuartz
To run R on a Mac operating system, XQuartz is required. You can install it by following this link, downloading it and following the installation instructions.
2.2.3 Install RStudio
To install RStudio we download it from the Posit website
Click on the Download RStudio Desktop link
Once downloaded, open the
.dmgfile and follow the installation instructions on your computer
2.3 Installing R and RStudio installation issues
If your installation for R and RStudio did not work, this is likely because your computer is running an older operating system. In these cases you will have to install an older version of the software.
It will be simplest, if you have faced installation issues, for you to come to a R pre-sessional workshop for support. See Section 6.1 for more information.
3 Why R?
LSE Statistics and methodology courses primarily use R. This is because R is a excellent tool for:
- Statistics
- Data handling (i.e. cleaning and manipulating data)
- Visualisations, interactive graphics, and dashboards
- Reporting (i.e. academic writing as well as building websites)
R is an open-source tool, which means you do not need to buy a licence in order to use it, and is a popular programming language, as shown in the PYPL index from 2023
Some cool things you can do in R:
This webpage was built using R. If you are interested, come to the pre-sessional workshops and chat to one of the instructors about it!
4 First steps with R
If you have not done so already, open RStudio!
Windows users:
- Type
RStudioin the search bar (bottom left, next to windows symbol) - If you prefer, open the start menu (click windows symbol), then scroll until you find RStudio
Mac users:
- Hit command + space and type in
RStudio - If you prefer to look for RStudio in an applications folder (open finder and select Applications on the left panel), you should be looking for this icon:
When RStudio opens you should see a layout with 3 panels, similar to the image below.
The largest panel on the left with the > is the console. On the bottom right there is the files/plot panel, and top right is the environment panel.
If your installation of R and RStudio has worked, first RStudio should be open, and second you should see a message in your console panel telling you the version of R you have installed, like the image below.
There are three ways of running R code: console, scripts and R Markdown. In this tutorial we will cover the console and scripts.
The best way to get comfortable with a software is to start using it! We will run through series of exercises which will help you get more comfortable writing and running R code. The exercises include:
- Performing some calculations using R
- Convert your height from centimetres to feet and metres
- Calculate body mass index (BMI) and waist-to-hip ratio
4.1 Exercise 1 - Running code from the console
The first thing we want to try is to run code from the console and see what happens. To run code from the console you type the code and press enter. Remember the console has the > symbol.
In RStudio, in the console, try the following calculations:
- Sum of 5 and 14.
- Divide 9.6 by 1.6
- 3 to the power of 12 subtracted from 4. Hint: use brackets
(x-y)^z - Divide 22 by 36 and multiply the result by 100
- The remainder of 55 divided by 2
The output for each question should be:
- [1] 19
- [1] 6
- [1] 512
- [1] 61.1111111111111
- [1] 1
| Operator | Symbol |
|---|---|
| Plus | + |
| Divide | / |
| Subtract | - |
| Multiply | * |
| Power | ^ |
| Remainder (modulus) | %% |
4.2 Exercise 2 - Assigning variables (still in the console)
In R, when we want to keep data and re-use it later, we assign that data to a name. There are two ways of doing this. We can use the arrow like <- or the = symbol; the arrow is most commonly used in R.
If we wanted to assign our numbers from one of our previous calculations and use them for a calculation we would do: a <- 9.6 and b <- 1.6, then a / b. These are called variables.
In your console:
- Assign 5 to
a - Assign 14 to
b - Calculate the sum of
aandb, and assign the result toc - Type
cand hit enter in your console. What happened?
In your environment panel, you should see:
4.3 Exercise 3 - making a new R script
Scripts are a useful way of remembering what we have done previously, allowing us to save code and share it with others.
In this exercise we are going to open a script, and save it.
- We have a few options to open a script, the simplest is to use a shortcut:
shift + command/ctrl + N. You can also open it manually by going the top left corner of RStudio, you should see a paper icon with a plus symbol. Click on it and select R Script - Now the script is open, save the script as something like
r-pre-sessional.R. There are a few ways of doing this, pressingcommand/ctrl + sis the simplest method - Lets try and run some code. In this example, you want to convert your running time in minutes to seconds. In the script, type or copy the following code:
- Now run the code! There are two main ways of doing this. First, we can highlight the code, and click the
Runbutton near the top centre right of RStudio. Second, we can put our cursor on each line and usecommand/ctrl + enterto run the code line by line. - The result of your code will appear in the console and should look like: [1] 1580.4
We will write more code in this script in the following exercise!
In the code example, we have hash tags (#). These are comments, which allow us to write non-code information. This is helpful to document what each step is doing.
4.4 Exercise 4 - height metrics conversion
In this exercise we are going to convert your height from centimetres to feet and meters. Below are some formulas to help you make the calculations in the exercises.
centimetres to feet = [height in cm X 0.0328084]
centimetres to meters = [height in cm / 100]
feet to meters = [height in ft X 0.3048]
In the script we made in exercise 3, try the following exercises:
- Make a variable called
my_height, and assign 195 as the height - Convert the value of
my_heightfrom centimetres to feet. Make a new variable calledmy_height_ft, and assign the calculation ofmy_heightin cm to feet - Print your
my_height_ftvariable - Convert the value of
my_heightfrom centimetres to meters. Make a new variable calledmy_height_m, and assign the calculation ofmy_heightin centimetres to meters - Print your
my_height_mvariable - Convert the value of
my_height_ftfrom feet to meters. Make a new variable calledmy_height_ft_m, and assign the calculation ofmy_heightin centimetres to meters - Print your
my_height_ft_mvariable - Now try and do the calculations for your own height, or an estimate if you are not sure! You can just change the value of
my_height
Using a value of my_height as 195cm, we would expect to get the outputs of:
- centimetres to feet: [1] 6.397638
- centimetres to meters: [1] 1.95
- feet to meters: [1] 1.9500000624
When we say “print a value”, which is to display the output of an object, we mean for you to type a variable and run it, such as typing my_height and pressing command/ctrl + enter to view the result in the console
4.5 Exercise 5 - loading R scripts
In the next two exercises we will be loading a pre-prepared script and doing some coding with it.
- Click on the Download R file button and save the file where you saved your other R script
- Now open the file into R. You should be able to use the file menu to achieve this:
File > Open File... - Within the file you should see some pre-written code. Run all the code
- What output appeared in the console? Assuming this person is a man, what would their health risk be according to the below table?
| Health risk | Women | Men |
|---|---|---|
| low | 0.80 or lower | 0.95 or lower |
| moderate | 0.81-0.85 | 0.96-1.0 |
| high | 0.86 or higher | 1.0 or higher |
In the next exercise, you will do another health related calculation, working out how to calculate body mass index (BMI)
The waist-to-hip ratio metric is another measure of health that is designed to look for people at higher risk of conditions like heart disease or type 2 diabetes.
4.6 Exercise 6 - Body mass index (BMI) calculation
Use R to work out the body mass index (BMI) of someone who is 94kg, and 1.95m tall.
In the script we loaded in exercise 5:
- Assign the variables of weight and height
- Assign the variable of BMI, and calculate the BMI based of the weight and height variables
- Print the outcome
- Add comments on what each line of code is doing
You can find the formula for BMI on the NHS website
You should get an output of [1] 24.7205785667324
5 Note on R and RStudio
You might be asking yourself, why have I installed R and RStudio?
An abridged answer to this question is R is the language we will be using, and RStudio is the environment in which we will be using R.
The unabridged answer is that R is a computer language, which means it cannot be opened like other computer applications such as an internet explorer (Chrome or Firefox) or Microsoft Word. Instead, to use R you write commands in the R language and ask your computer to interpret them. Previously, this would involve typing commands into a terminal application, or writing a script in a text editing software and running it through a terminal application. This is not the easiest way of writing R code, and is where RStudio comes in!
RStudio is an application, like Google Chrome or Firefox, which means it is easy to open on your computer. RStudio is a popular tool for using R, as it provides an pleasant interface for you to use R, with helpful features like the auto-completion, file management, and an environment panel to show/explore your data.
The terminal is a program that you use to type in commands that are then executed by your computer’s operating system. It is a text input/output environment
6 What are my next steps?
If you are on the modules show below, it is recommended you attend the in-person pre-sessional training workshops the Digital Skills Lab will be running through September to early October.
- MY452/552
- MY455/555
- MY472
- MY457/557
- MY474/574
- MY461/561
- MY459/559
- DS105
- DS202
Students not on these modules are very welcome to join the in-person pre-sessional training, particularly if you have struggled to get R installed, found the exercises difficult, or want to get a better understanding of R.
6.1 R Pre-Sessional in-person workshops
The pre-sessional workshops are two hour long, in-person practical workshops to help you learn R. You will be working through practical exercises, designed to build your knowledge in R, while being supported by a team of data science trainers.
The content for these workshops will cover the key fundamental knowledge needed for using the R programming language. The content is split into worksheets which gradually progress you through topics. These topics include, but are not limited to:
Working with different data types in R such as numerical, text, categorical, data frames (spreadsheet like data structures), matrices, and lists
Learning to manipulate these data types to extract what information you need, and perform operations on them
Using conditional operations to manipulate data
Using functions, built in code, to perform a wide array of operations like finding the mean
Loading datasets into R, and handling some common issues
Automating code, using tools like a for loop
Streamline your code by writing your own functions
These workshops will be run on a regular basis through September and early October. Dates and times for the in-person workshops:
| Date | Time | Location |
|---|---|---|
| 12th September | 10-12 | LSE Life Workspace 4 |
| 14th September | 10-12 | LSE Life Workspace 4 |
| 15th September | 10-12 | LSE Life Workspace 4 |
| 26th September | 1-3 | LSE Life Workspace 4 |
| 27th September | 10-12 | LSE Life Workspace 4 |
| 29th September | 10-12 | LSE Life Workspace 4 |
| 2nd October | 10-12 | LSE Life Workspace 4 |
| 3rd October | 10-12 | LSE Life Workspace 4 |
| 4th October | 10-12 | LSE Life Workspace 4 |
Attendance to the workshops is through booking only. To book onto the workshops, you will need to follow this booking link
These workshops will be popular and there will be a maximum capacity. If you can no longer attend, please cancel your booking so another student can book.
6.2 What other support is available?
Outside the pre-sessional workshops, the Digital Skills Lab will be running R workshops from October-December, then January-March, and again May-July.
There will also be drop-in advice and 1-2-1 services available throughout the year.
You can find out more information on the courses and support the Digital Skills Lab offers via our webpage.
For help or other general queries please contact digital.skills.lab@lse.ac.uk.
7 Accessing and opening the in-person pre-sessional materials (optional extra)
As an optional extra, you can prepare for the in-person workshops by accessing the materials for the workshops. Below is some information on how to do this.
7.1 Access to the DSL-R-Presessional team on MS Teams
The materials for the workshops will be hosted/stored on MS Teams. They are R Markdown files.
- Go to the Teams overview in MS Teams, which can be accessed from the sidebar on the left.
- Select the Join or create team option on the top right.
- Enter the team code cfwj2ed
- Click Join Team.
7.2 Download the R Markdown files
- Go to the Teams overview in MS Teams, which can be accessed from the sidebar on the left.
- Select the DSL-R-Presessional team.
- Select the R Markdown Notebooks channel.
- Select the Files tab to access the R markdown notebooks.
- Select all items in the folder.
- Download the files. You might need to select the three dot icon to access the Download option.
- Click Download to download all R markdown notebooks as a zip file.
An R Markdown file has sections of text and code together in a single document. This is helpful for tutorials, documentation, and writing reports.
You’ll be introduced to R Markdown in the workshops. An in-depth description can be found in the R for Data Science open source book
7.3 Opening the R Markdown files
Opening a R Markdown file is very similar to opening a script in R using File > Open File...
How to use and work with R Markdown files will be covered in the in-person workshops.
If you struggle with any of these steps, just come along to the workshops and a trainer can help you.
8 Final remarks
This tutorial was written by the Digital Skills Lab (DSL) in support for the Statistics and Methodology departments, and the Data Science Institute. We hope that you found it helpful in getting started with R.